//
//  YYT_RegisterViewController.m
//  AudioVisualFeast
//
//  Created by 温晓宇 on 16/1/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YYT_RegisterViewController.h"
#import "YYT_RegisterView.h"
@interface YYT_RegisterViewController ()
<
    YYT_RegisterViewDelegate
>

@property (nonatomic, strong)YYT_RegisterView *regisView;

@end

@implementation YYT_RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"注册";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backButton:)];
    
    self.regisView = [[YYT_RegisterView alloc] initWithFrame:self.view.bounds];
    _regisView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_regisView];
    _regisView.delegate = self;
}

- (void)regisUser{
    if ([_regisView.userText.text isEqualToString:@""] || [_regisView.passwordText.text isEqualToString:@""] || [_regisView.ConfirmText.text isEqualToString:@""] || [_regisView.emailText.text isEqualToString:@""]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"不能为空!" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
    }else if (![_regisView.passwordText.text isEqualToString:_regisView.ConfirmText.text]){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"两个密码不相同,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
    }else{
    BmobUser *bUser = [[BmobUser alloc] init];
    [bUser setUsername:self.regisView.userText.text];
    [bUser setPassword:self.regisView.passwordText.text];
    [bUser setEmail:self.regisView.emailText.text];
    [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
        if (isSuccessful){
            NSLog(@"Sign up successfully");
            [self dismissViewControllerAnimated:YES completion:nil];
            [self.delegate dismissLogin];
        } else {
            NSLog(@"%@",error);
        }
    }];
    }

}

-(void)backButton:(UIBarButtonItem *)btn{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
